using System;
using System.Data;
using System.Collections;
using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.CarInsurance
{
    public class CarMakeDB
    {
        public CarMakeDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public int Insert(CarMake carmake)
        {
            MyDatabase _db = new MyDatabase();
            int _result = -1;
            string _spName = "sprocCarMakeInsertUpdateSingleItem";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
            _sPara.Add("@CarMakeAutoId", carmake.CarMakeAutoId);
            _sPara.Add("@CarMakeId", carmake.CarMakeId);
            _sPara.Add("@Description", carmake.Description);
            _sPara.Add("@RefCEUserId", carmake.RefCEUserId);
            _sPara.Add("@Status", carmake.Status);
            _sPara.Add("@Info1", carmake.Info1);
            _sPara.Add("@Info2", carmake.Info2);
            _sPara.Add("@Info3", carmake.Info3);
            _sPara.Add("@Info4", carmake.Info4);
            _sPara.Add("@Info5", carmake.Info5);
            #endregion

            _result = Convert.ToInt32(_db.ExecuteSP(_spName, _sPara));

            return _result;
        }

        public CarMake GetCarMake(int CarMakeId)
        {
            CarMake _carmake = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarMakeSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarMakeId", CarMakeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carmake = new CarMake();
                    _carmake = FillDataRecord(_dt, 0);
                }
            }
            return _carmake;
        }

        public int GetNextId(string CarMakeDescription)
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarMakeSelectMaxItem";
            int _result = 0;
            SortedList _spara = new SortedList();
            if (CarMakeDescription.Length > 0)
            {
                _spara.Add("@Description", CarMakeDescription.Substring(0, 1));
            }
            else
            {
                _spara.Add("@Description", "");
            }

            _result = Convert.ToInt32(_db.ExecuteSP(_spName, _spara));
            
            return _result;
        }

        public int GetCarMakeNextId(string CarMakeDescription)
        {
            int _result = GetNextId(CarMakeDescription);

            if (_result == 0)
            {
                _result = GetNextId("");
                if (_result % 100 != 0)
                {
                    int _value = _result % 100;
                    _result = _result - _value;
                }
            }

            return _result;
        }

        public bool CheckCarMakeId(int CarMakeId, out int CarMakeAutoId)
        {
            CarMake _carmake = new CarMake();
            bool _result = false;
            _carmake = GetCarMake(CarMakeId);
            CarMakeAutoId = 0;
            if (_carmake != null)
            {
                CarMakeAutoId = _carmake.CarMakeAutoId;
                _result = true;
            }

            return _result;
        }

        public CarMakeList GetCarMakeList()
        {
            CarMakeList _carmakelist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarMakeSelectItemList";

            SortedList _spara = new SortedList();
            
            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carmakelist = new CarMakeList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarMake _carmake = new CarMake();
                        _carmake = FillDataRecord(_dt, i);
                        _carmakelist.Add(_carmake);
                    }
                }
            }
            return _carmakelist;
        }

        public CarMakeList SearchCarMakeList(string CarMake)
        {
            CarMakeList _carmakelist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCarMakeSearchItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarMakeDescription", CarMake);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carmakelist = new CarMakeList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarMake _carmake = new CarMake();
                        _carmake = FillDataRecord(_dt, i);
                        _carmakelist.Add(_carmake);
                    }
                }
            }
            return _carmakelist;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }

        private CarMake FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarMake myCarMake = new CarMake();
            myCarMake.CarMakeAutoId = Convert.ToInt32(myDataTable.Rows[RowId]["CarMakeAutoId"].ToString());
            myCarMake.CarMakeId = Convert.ToInt32(myDataTable.Rows[RowId]["CarMakeId"].ToString());
            myCarMake.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myCarMake.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarMake.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            return myCarMake;
        }
    }
}